package com.rslai.base.tool.servertest.command;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.rslai.base.tool.servertest.context.Context;
import com.rslai.base.tool.servertest.fastjson.QunitDoubleSerializer;
import com.rslai.base.tool.servertest.response.Response;
import com.rslai.base.tool.servertest.util.PropertyUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;

public class PrintStepCommand extends StepCommand {
    String result;

    public Response doExecute(Response param, Context context)
            throws Throwable {
        if (isOnJenkins()) return param;
        Boolean jsonWriteOriginalDoubleValue = Boolean.valueOf(PropertyUtils.getProperty("json_write_original_double_value", "false"));
        SerializeConfig config = new SerializeConfig();
        if (jsonWriteOriginalDoubleValue.booleanValue()) {
            config.setAsmEnable(false);
            config.put(Double.class, QunitDoubleSerializer.INSTANCE);
        }
        this.result = JSON.toJSONString(param, config, new SerializerFeature[]{SerializerFeature.WriteMapNullValue});
        this.logger.info(this.result);
        return param;
    }

    private boolean isOnJenkins() {
        String jenkinsHome = System.getProperty("JENKINS_HOME");
        return StringUtils.isNotBlank(jenkinsHome);
    }

    public StepCommand doClone() {
        return new PrintStepCommand();
    }

    public Map<String, Object> toReport() {
        Map details = new HashMap();
        details.put("stepName", "打印上一步执行器的结果信息:");
        details.put("name", this.result);
        List params = new ArrayList();
        details.put("params", params);
        return details;
    }
}
